package com.example.mvvmtemplate.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mvvmtemplate.R;
import com.example.mvvmtemplate.utli.edit.InputMethodUtils;
import com.example.mvvmtemplate.utli.statusbar.StatusBarUtil;

public abstract class BaseActivity extends AppCompatActivity {

    public Context mContext;

    public abstract int getLayoutResID();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //防止截屏攻击风险
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        //仿系统自带短信界面 可以完全漂浮在软键盘之上
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResID());
        //设置状态栏
        StatusBarUtil.setStatusBarColor(this);
        mContext = getApplicationContext();
    }

    /**
     * 重写startActivity
     */
    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
        enterTransitionAnim();
    }

    /**
     * 重写startActivity
     */
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
        enterTransitionAnim();
    }

    public void startActivityForResult(Class<?> cls, int requestCode) {
        Intent intent = new Intent(this, cls);
        startActivityForResult(intent, requestCode);
        enterTransitionAnim();
    }

    public void startActivityForResult(Class<?> cls, int requestCode, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        startActivityForResult(intent, requestCode, bundle);
        enterTransitionAnim();
    }

    /**
     * 进入Activity过渡动画
     */
    private void enterTransitionAnim() {
        overridePendingTransition(R.anim.left_in, R.anim.none);
    }

    /**
     * 退出Activity过渡动画
     */
    private void quitTransitionAnim() {
        overridePendingTransition(R.anim.none, R.anim.left_out);
    }

    @Override
    public void finish() {
        super.finish();
        quitTransitionAnim();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        quitTransitionAnim();
    }

    /**
     * 点击空白处隐藏输入法
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v instanceof EditText) {
                Rect outRect = new Rect();
                v.getGlobalVisibleRect(outRect);
                if (!outRect.contains((int) event.getX(), (int) event.getY())) {
                    InputMethodUtils.hideSoftInput(mContext, v);
                }
            }
        }
        return super.dispatchTouchEvent(event);
    }

    public void showToast(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
